
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>逐行解读reactive如何构建Map和Set的响应式对象 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3_register_side_effect.html" />
    
    
    <link rel="prev" href="1_reactive_object_array.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../1_online_rendering/0_intro.html">
            
                <a href="../1_online_rendering/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1_online_rendering/1_start_from_static_view.html">
            
                <a href="../1_online_rendering/1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                <a href="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../1_online_rendering/4_directives/2_event_binding.html">
            
                <a href="../1_online_rendering/4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                <a href="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../1_online_rendering/4_directives/4_ref.html">
            
                <a href="../1_online_rendering/4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../1_online_rendering/5_tips_for_template.html">
            
                <a href="../1_online_rendering/5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="1_reactive_object_array.html">
            
                <a href="1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="2_reactive_map_set.html">
            
                <a href="2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="3_register_side_effect.html">
            
                <a href="3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="4_cleanup_algorithm.html">
            
                <a href="4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="5_smi.html">
            
                <a href="5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="6_trigger_and_scheduler.html">
            
                <a href="6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >逐行解读reactive如何构建Map和Set的响应式对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;map&#x548C;set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><b>1. </b>&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;Map&#x548C;Set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</a></li><ul><li><span class="title-icon "></span><a href="#mapweakmapsetweakset&#x7684;&#x64CD;&#x4F5C;"><b>1.1. </b>Map/WeakMap/Set/WeakSet&#x7684;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;"><b>1.2. </b>&#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;</a></li><ul><li><span class="title-icon "></span><a href="#map&#x7684;get&#x65B9;&#x6CD5;"><b>1.2.1. </b>Map&#x7684;get&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#map&#x548C;set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;"><b>1.2.2. </b>Map&#x548C;Set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;</a></li><li><span class="title-icon "></span><a href="#map&#x548C;set&#x7684;has&#x65B9;&#x6CD5;"><b>1.2.3. </b>Map&#x548C;Set&#x7684;has&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#set&#x7684;add&#x65B9;&#x6CD5;"><b>1.2.4. </b>Set&#x7684;add&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#map&#x7684;set&#x65B9;&#x6CD5;"><b>1.2.5. </b>Map&#x7684;set&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#deleteentry&#x65B9;&#x6CD5;"><b>1.2.6. </b>deleteEntry&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#map&#x548C;set&#x7684;clear&#x65B9;&#x6CD5;"><b>1.2.7. </b>Map&#x548C;Set&#x7684;clear&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#map&#x548C;set&#x7684;foreach&#x65B9;&#x6CD5;"><b>1.2.8. </b>Map&#x548C;Set&#x7684;forEach&#x65B9;&#x6CD5;</a></li></ul><li><span class="title-icon "></span><a href="#&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;"><b>1.3. </b>&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;</a></li><ul><li><span class="title-icon "></span><a href="#&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;iterable-protocol"><b>1.3.1. </b>&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;(iterable protocol)</a></li><li><span class="title-icon "></span><a href="#&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;iterator-protocol"><b>1.3.2. </b>&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;(iterator protocol)</a></li></ul><li><span class="title-icon "></span><a href="#&#x5C0F;&#x7ED3;"><b>1.4. </b>&#x5C0F;&#x7ED3;</a></li></ul></ul></div><h1 id="&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;map&#x548C;set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><a name="&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;map&#x548C;set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;" class="anchor-navigation-ex-anchor" href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;map&#x548C;set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;Map&#x548C;Set&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</h1>
<p>&#x672C;&#x7BC7;&#x6211;&#x4EEC;&#x4F1A;&#x7EE7;&#x7EED;&#x63A2;&#x7D22;<code>reactive</code>&#x51FD;&#x6570;&#x4E2D;&#x5BF9;<code>Map/WeakMap/Set/WeakSet</code>&#x5BF9;&#x8C61;&#x7684;&#x4EE3;&#x7406;&#x5B9E;&#x73B0;&#x3002;</p>
<h2 id="mapweakmapsetweakset&#x7684;&#x64CD;&#x4F5C;"><a name="mapweakmapsetweakset&#x7684;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#mapweakmapsetweakset&#x7684;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. Map/WeakMap/Set/WeakSet&#x7684;&#x64CD;&#x4F5C;</h2>
<p>&#x7531;&#x4E8E;WeakMap&#x548C;WeakSet&#x5206;&#x522B;&#x662F;Map&#x548C;Set&#x7684;&#x4E0D;&#x5F71;&#x54CD;GC&#x6267;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EA;&#x7814;&#x7A76;Map&#x548C;Set&#x5373;&#x53EF;&#x3002;</p>
<p>Set&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</p>
<ul>
<li><code>size: number</code> &#x4E3A;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;(accessor property)&#xFF0C;&#x8FD4;&#x56DE;Set&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x503C;&#x7684;&#x4E2A;&#x6570;</li>
<li><code>add(value: any): Set</code> &#x5411;Set&#x5BF9;&#x8C61;&#x961F;&#x5C3E;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</li>
<li><code>clear(): void</code> &#x79FB;&#x9664;Set&#x5BF9;&#x8C61;&#x5185;&#x6240;&#x6709;&#x5143;&#x7D20;</li>
<li><code>delete(value: any): boolean</code> &#x79FB;&#x9664;Set&#x4E2D;&#x4E0E;&#x5165;&#x53C2;&#x503C;&#x76F8;&#x540C;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x79FB;&#x9664;&#x6210;&#x529F;&#x5219;&#x8FD4;&#x56DE;true</li>
<li><code>has(value: any): boolean</code> &#x5224;&#x65AD;Set&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x4E0E;&#x5165;&#x53C2;&#x503C;&#x76F8;&#x540C;&#x7684;&#x5143;&#x7D20;</li>
<li><code>values(): Iterator</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B;Set&#x5BF9;&#x8C61;&#x4E2D;&#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;</li>
<li><code>keys(): Iterator</code> &#x548C;<code>values(): Iterator</code>&#x4E00;&#x6837;&#x7684;&#x529F;&#x6548;</li>
<li><code>@@iterator</code> &#x548C;<code>values(): Iterator</code>&#x4E00;&#x6837;&#x7684;&#x529F;&#x6548;&#xFF0C;<code>for of</code>&#x4E2D;&#x8C03;&#x7528;</li>
<li><code>entries(): Iterator</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B;Set&#x5BF9;&#x8C61;&#x4E2D;&#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;&#xFF0C;&#x4F46;&#x4E3A;&#x4E0E;Map&#x4F7F;&#x7528;&#x4E00;&#x81F4;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x8FD4;&#x56DE;&#x7684;&#x5185;&#x5BB9;&#x4E3A;<code>[value, value]</code></li>
<li><code>forEach(callbackFn: { (value: any, set: Set) =&gt; any } [, thisArg])</code> &#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x904D;&#x5386;Set&#x5BF9;&#x8C61;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</li>
</ul>
<p>Map&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</p>
<ul>
<li><code>size: number</code> &#x4E3A;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;(accessor property)&#xFF0C;&#x8FD4;&#x56DE;Set&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x503C;&#x7684;&#x4E2A;&#x6570;</li>
<li><code>set(key: any, value: any): Map</code> &#x5411;Map&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x6216;&#x66F4;&#x65B0;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x952E;&#x7684;&#x503C;</li>
<li><code>clear(): void</code> &#x79FB;&#x9664;Map&#x5BF9;&#x8C61;&#x5185;&#x6240;&#x6709;&#x952E;&#x503C;&#x5BF9;</li>
<li><code>delete(key: any): boolean</code> &#x79FB;&#x9664;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x79FB;&#x9664;&#x6210;&#x529F;&#x5219;&#x8FD4;&#x56DE;true</li>
<li><code>has(key: any): boolean</code> &#x5224;&#x65AD;Map&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x952E;&#x4E0E;&#x5165;&#x53C2;&#x503C;&#x76F8;&#x540C;&#x7684;&#x952E;&#x503C;&#x5BF9;</li>
<li><code>values(): Iterator</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x6240;&#x6709;&#x503C;</li>
<li><code>keys(): Iterator</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x6240;&#x6709;&#x952E;</li>
<li><code>@@iterator</code> &#x548C;<code>entries(): Iterator</code>&#x4E00;&#x6837;&#x7684;&#x529F;&#x6548;&#xFF0C;<code>for of</code>&#x4E2D;&#x8C03;&#x7528;</li>
<li><code>entries(): Iterator</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x6240;&#x6709;&#x952E;&#x503C;&#x5BF9;</li>
<li><code>forEach(callbackFn: { (value: any, key: any, map: Map) =&gt; any } [, thisArg])</code> &#x6309;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#x904D;&#x5386;Map&#x5BF9;&#x8C61;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x952E;&#x503C;&#x5BF9;</li>
<li><code>get(key: any): any</code> &#x8FD4;&#x56DE;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6307;&#x5B9A;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#xFF0C;&#x82E5;&#x6CA1;&#x6709;&#x5219;&#x8FD4;&#x56DE;<code>undefined</code></li>
</ul>
<h2 id="&#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;"><a name="&#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;" class="anchor-navigation-ex-anchor" href="#&#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x9010;&#x884C;&#x770B;&#x4EE3;&#x7801;&#x6211;&#x662F;&#x8BA4;&#x771F;&#x7684;</h2>
<pre><code class="lang-ts"><span class="hljs-comment">// reactive.ts</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> mutableCollectionHandlers: ProxyHandler&lt;CollectionTypes&gt; = {
  <span class="hljs-keyword">get</span>: <span class="hljs-comment">/*#__PURE__*/</span> createInstrumentationGetter(<span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>)
}
</code></pre>
<p>&#x7531;&#x4E8E;Map/Set&#x4E0D;&#x50CF;Object&#x6216;Array&#x90A3;&#x6837;&#x53EF;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x5C5E;&#x6027;&#x8BBF;&#x95EE;&#x7684;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x5176;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;<code>add</code>,<code>has</code>,<code>delete</code>&#x64CD;&#x4F5C;&#xFF0C;&#x56E0;&#x6B64;&#x9700;&#x8981;&#x50CF;&#x5904;&#x7406;Array&#x7684;<code>slice</code>&#x7B49;&#x65B9;&#x6CD5;&#x90A3;&#x6837;&#x4EE3;&#x7406;Map/Set&#x7684;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// collectionHandlers.ts</span>

<span class="hljs-keyword">type</span> MapTypes = Map&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">any</span>&gt; | WeakMap&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">any</span>&gt;
<span class="hljs-keyword">type</span> SetTypes = Set&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">any</span>&gt; | WeakSet&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">any</span>&gt;

<span class="hljs-comment">// &#x4EE3;&#x7406;Map/Set&#x539F;&#x751F;&#x7684;&#x65B9;&#x6CD5;</span>
<span class="hljs-comment">// &#x6CA1;&#x6709;&#x4EE3;&#x7406;&#x8FD4;&#x56DE;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x65B9;&#x6CD5;&#xFF1F;&#xFF1F;</span>
<span class="hljs-keyword">const</span> mutableInstrumentations = {
  <span class="hljs-keyword">get</span>(<span class="hljs-keyword">this</span>: MapTypes, key: unknown) {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">get</span>(<span class="hljs-keyword">this</span>, key)
  }
  <span class="hljs-keyword">get</span> size() {
    <span class="hljs-comment">// &#x539F;&#x751F;&#x7684;size&#x5C5E;&#x6027;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">return</span> size(<span class="hljs-keyword">this</span> as unknown as IterableCollections)
  },
  has,
  add,
  <span class="hljs-keyword">set</span>,
  <span class="hljs-keyword">delete</span>: deleteEntry, <span class="hljs-comment">// delete &#x662F;&#x5173;&#x952E;&#x5B57;&#x4E0D;&#x80FD;&#x4F5C;&#x4E3A;&#x53D8;&#x91CF;&#x6216;&#x51FD;&#x6570;&#x540D;&#x79F0;</span>
  clear,
  forEach: createForEach(<span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>)
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createInstrumentationGetter</span>(<span class="hljs-params">isReadonly: <span class="hljs-built_in">boolean</span>, shallow: <span class="hljs-built_in">boolean</span></span>) </span>{
  <span class="hljs-keyword">const</span> instrumentations = mutableInstrumentations

  <span class="hljs-keyword">return</span> (
    target: CollectionTypes,
    key: <span class="hljs-built_in">string</span> | symbol,
    receiver: CollectionTypes
  ) =&gt; {
    <span class="hljs-keyword">if</span> (key === ReactiveFlags.IS_REACTIVE) {
      <span class="hljs-keyword">return</span> !isReadonly
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key === ReactiveFlags.IS_READONLY) {
      <span class="hljs-keyword">return</span> isReadonly
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key === ReactiveFlags.RAW) {
      <span class="hljs-keyword">return</span> target
    }

    <span class="hljs-comment">// &#x4EE3;&#x7406;Map/WeakMap/Set/WeakSet&#x7684;&#x5185;&#x7F6E;&#x65B9;&#x6CD5;</span>
    <span class="hljs-keyword">return</span> Reflect.get(
      hasOwn(instrumentations, key) &amp;&amp; key <span class="hljs-keyword">in</span> target
        ? instrumentations
        : target,
      key,
      receiver
    )
  }
}
</code></pre>
<p><em>TypeScript&#x5C0F;&#x8BFE;&#x5802;</em>&#xFF1A;<code>as</code>&#x65AD;&#x8A00;&#x2014;&#x2014;<code>this as unknown as IterableCollections</code>
&#x5728;TypeScript&#x4E2D;&#x53EF;&#x901A;&#x8FC7;&#x7C7B;&#x578B;&#x58F0;&#x660E;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;(&#x5176;&#x4E2D;&#x5305;&#x542B;&#x590D;&#x5408;&#x7C7B;&#x578B;)&#xFF0C;&#x800C;&#x7C7B;&#x578B;&#x63A8;&#x5BFC;&#x5219;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#x4E2D;&#x53F3;&#x4FA7;&#x5B57;&#x9762;&#x91CF;&#x63A8;&#x5BFC;&#x51FA;&#x53D8;&#x91CF;&#x7684;&#x5B9E;&#x9645;&#x7C7B;&#x578B;&#xFF0C;&#x6216;&#x901A;&#x8FC7;&#x5F53;&#x524D;&#x53D8;&#x91CF;&#x4F7F;&#x7528;&#x7684;&#x573A;&#x666F;&#x63A8;&#x5BFC;&#x51FA;&#x5F53;&#x524D;&#x5B9E;&#x9645;&#x7C7B;&#x578B;(&#x5C24;&#x5176;&#x662F;&#x5B9A;&#x4E49;&#x4E3A;&#x590D;&#x5408;&#x7C7B;&#x578B;)&#x3002;&#x4F46;&#x6709;&#x65F6;&#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x6267;&#x884C;&#x7CBE;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x63A8;&#x5BFC;&#xFF0C;&#x8FD9;&#x65F6;&#x5F00;&#x53D1;&#x8005;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>as</code>&#x65AD;&#x8A00;&#x544A;&#x77E5;TypeScript&#x7F16;&#x8BD1;&#x5668;&#x8BE5;&#x53D8;&#x91CF;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x8303;&#x56F4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;(&#x8981;&#x76F8;&#x4FE1;&#x81EA;&#x5DF1;&#x4E00;&#x5B9A;&#x6BD4;&#x7F16;&#x8BD1;&#x5668;&#x66F4;&#x4E86;&#x89E3;&#x81EA;&#x5DF1;&#x7684;&#x4EE3;&#x7801;:D)&#x3002;
&#x90A3;&#x4E48;<code>as unknown</code>&#x5373;&#x8868;&#x793A;&#x5C06;&#x7C7B;&#x578B;&#x4FEE;&#x6539;&#x4E3A;<code>unknown</code>&#xFF0C;&#x90A3;&#x4E48;&#x7C7B;&#x578B;&#x4E3A;<code>unknown</code>&#x662F;&#x8868;&#x793A;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;<code>unknown</code>&#x662F;TypeScript3.0&#x5F15;&#x5165;&#x7684;top type(&#x4EFB;&#x4F55;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x90FD;&#x662F;&#x5B83;&#x7684;subtype)&#xFF0C;&#x610F;&#x5728;&#x63D0;&#x4F9B;&#x4E00;&#x79CD;&#x66F4;&#x5B89;&#x5168;&#x7684;&#x65B9;&#x5F0F;&#x66FF;&#x4EE3;<code>any</code>&#x7C7B;&#x578B;(<code>any</code>&#x7C7B;&#x578B;&#x662F;top type&#x4E5F;&#x662F;bottom type&#xFF0C;&#x4F7F;&#x7528;&#x5B83;&#x610F;&#x5473;&#x548C;&#x7ED5;&#x8FC7;&#x7C7B;&#x578B;&#x68C0;&#x67E5;)&#xFF0C;&#x5177;&#x6709;&#x5982;&#x4E0B;&#x7279;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x4EFB;&#x4F55;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x90FD;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;<code>unknown</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;</li>
<li><code>unknown</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x53EA;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;<code>any</code>&#x6216;<code>unknown</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;</li>
<li>&#x5982;&#x679C;&#x4E0D;&#x5BF9;<code>unknown</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x6267;&#x884C;&#x7C7B;&#x578B;&#x6536;&#x7F29;&#xFF0C;&#x5219;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-comment">// 1. &#x4EFB;&#x4F55;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x90FD;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;`unknown`&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF; </span>
<span class="hljs-keyword">let</span> uncertain: unknown = <span class="hljs-string">&apos;Hello&apos;</span>
uncertain = <span class="hljs-number">12</span>
uncertain = { hello: () =&gt; <span class="hljs-string">&apos;Hello&apos;</span> }

<span class="hljs-comment">// 2.`unknown`&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x53EA;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;`any`&#x6216;`unknown`&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF; </span>
<span class="hljs-keyword">let</span> uncertain: unknown = <span class="hljs-string">&apos;Hello&apos;</span>
<span class="hljs-keyword">let</span> noSure: <span class="hljs-built_in">any</span> = uncertain
<span class="hljs-keyword">let</span> notConfirm: unknown = uncertain

<span class="hljs-comment">// 3. &#x5982;&#x679C;&#x4E0D;&#x5BF9;`unknown`&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x6267;&#x884C;&#x7C7B;&#x578B;&#x6536;&#x7F29;&#xFF0C;&#x5219;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;</span>
<span class="hljs-keyword">let</span> uncertain = { hello: () =&gt; <span class="hljs-string">&apos;Hello&apos;</span> }
uncertain.hello() <span class="hljs-comment">// &#x7F16;&#x8BD1;&#x62A5;&#x9519; </span>
<span class="hljs-comment">// &#x901A;&#x8FC7;&#x65AD;&#x8A00;as&#x6536;&#x7F29;&#x7C7B;&#x578B;</span>
(uncertain as {hello: () =&gt; <span class="hljs-built_in">string</span>}).hello()

<span class="hljs-keyword">let</span> uncertain: unknown = <span class="hljs-string">&apos;Hello&apos;</span>
<span class="hljs-comment">// &#x901A;&#x8FC7;typeof&#x6216;instanceof&#x6536;&#x7F29;&#x7C7B;&#x578B;</span>
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> uncertain === <span class="hljs-string">&apos;string&apos;</span>) {
  uncertain.toLowerCase()
}
</code></pre>
<p>&#x90A3;&#x4E48;<code>as unknown</code>&#x540E;&#x7684;<code>as IterableCollections</code>&#x610F;&#x56FE;&#x5C31;&#x5341;&#x5206;&#x660E;&#x663E;&#x4E86;&#xFF0C;&#x5C31;&#x662F;&#x5BF9;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x6536;&#x7F29;&#x3002;<code>this as unknown as IterableCollections</code>&#x5176;&#x5B9E;&#x5C31;&#x662F;<code>as IterableCollections</code>&#x5566;&#x3002;</p>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x9010;&#x4E00;&#x770B;&#x770B;&#x4EE3;&#x7406;&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x5427;</p>
<h3 id="map&#x7684;get&#x65B9;&#x6CD5;"><a name="map&#x7684;get&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#map&#x7684;get&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. Map&#x7684;get&#x65B9;&#x6CD5;</h3>
<p><code>get</code>&#x65B9;&#x6CD5;&#x53EA;&#x6709;Map&#x5BF9;&#x8C61;&#x62E5;&#x6709;&#xFF0C;&#x56E0;&#x6B64;&#x5176;&#x4E2D;&#x4E3B;&#x8981;&#x601D;&#x8DEF;&#x662F;&#x4ECE;Map&#x5BF9;&#x8C61;&#x4E2D;&#x83B7;&#x53D6;&#x503C;&#xFF0C;&#x8DDF;&#x8E2A;&#x952E;&#x503C;&#x53D8;&#x5316;&#x540E;&#x5C06;&#x503C;&#x8F6C;&#x6362;&#x4E3A;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x8FD4;&#x56DE;&#x5373;&#x53EF;&#x3002;
&#x4F46;&#x7531;&#x4E8E;&#x8981;&#x5904;&#x7406;<code>readonly(reactive(new Map()))</code>&#x8FD9;&#x4E00;&#x573A;&#x666F;&#xFF0C;&#x6DFB;&#x52A0;&#x4E86;&#x5F88;&#x591A;&#x4E00;&#x65F6;&#x8BA9;&#x4EBA;&#x770B;&#x4E0D;&#x61C2;&#x7684;&#x4EE3;&#x7801;&#x800C;&#x5DF2;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> getProto = &lt;T extends CollectionTypes&gt;(v: T): <span class="hljs-built_in">any</span> =&gt; Reflect.getProrotypeOf(v)

<span class="hljs-comment">// &#x4EE3;&#x7406;Map/WeakMap&#x7684;get&#x65B9;&#x6CD5;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">get</span>(<span class="hljs-params">
  target: MapTypes, <span class="hljs-comment">// &#x6307;&#x5411;this&#xFF0C;&#x7531;&#x4E8E;Map&#x5BF9;&#x8C61;&#x5DF2;&#x7ECF;&#x88AB;&#x4EE3;&#x7406;&#xFF0C;&#x56E0;&#x6B64;this&#x4E3A;&#x4EE3;&#x7406;&#x4EE3;&#x7406;</span>
  key: unknown,
  isReadonly = <span class="hljs-literal">false</span>,
  isShallow = <span class="hljs-literal">false</span>
</span>) </span>{
  <span class="hljs-comment">/**
   * 1. &#x9488;&#x5BF9;readonly(reactive(new Map()))&#x7684;&#x60C5;&#x51B5;&#xFF0C;
   *    target&#x83B7;&#x53D6;&#x7684;&#x662F;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;rawTarget&#x7684;&#x662F;Map&#x5BF9;&#x8C61;
   * 2. &#x9488;&#x5BF9;reactive(new Map())&#x7684;&#x60C5;&#x51B5;&#xFF0C;
   *    target&#x548C;rawTarget&#x90FD;&#x662F;&#x6307;&#x5411;Map&#x5BF9;&#x8C61;
   */</span> 
  target = (target as <span class="hljs-built_in">any</span>)[ReactiveFlags.RAW]
  <span class="hljs-keyword">const</span> rawTarget = toRaw(target)
  <span class="hljs-comment">/**
   * &#x82E5;key&#x4E3A;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x548C;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x952E;&#x90FD;&#x4F1A;&#x88AB;&#x8DDF;&#x8E2A;&#xFF0C;&#x5373;
   * const key = { value: &apos;foo&apos; }
   * const pKey = reactive(key), 
   * const kvs = reactive(new Map())
   * kvs.set(pKey, 1)
   * 
   * effect(() =&gt; {
   *   console.log(&apos;pKey&apos;, kvs.get(pKey))
   * })
   * effect(() =&gt; {
   *   console.log(&apos;key&apos;, kvs.get(key))
   * })
   * 
   * kvs.set(pKey, 2)
   * // &#x56DE;&#x663E; pkey 2 &#x548C; key 2
   * kvs.set(key, 3)
   * // &#x56DE;&#x663E; key 2
   */</span>  
  <span class="hljs-keyword">const</span> rawKey = toRaw(key)
  <span class="hljs-keyword">if</span> (key !== rawKey) {
    !isReadonly &amp;&amp; track(rawTraget, TrackOpTypes.GET, key)
  }
  !isReadonly &amp;&amp; track(rawTraget, TrackOpTypes.GET, rawKey)

  <span class="hljs-comment">// &#x83B7;&#x53D6;Map&#x539F;&#x578B;&#x94FE;&#x4E0A;&#x7684;has&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5224;&#x65AD;&#x83B7;&#x53D6;&#x6210;&#x5458;&#x662F;&#x5426;&#x5B58;&#x5728;&#x4E8E;Map&#x5C0D;&#x8C61;&#x4E0A;</span>
  <span class="hljs-keyword">const</span> { has } = getProto(rawTarget)
  <span class="hljs-keyword">const</span> wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
  <span class="hljs-comment">/**
   * Map&#x5BF9;&#x8C61;&#x4E2D;&#x5B58;&#x5728;&#x5219;&#x4ECE;Map&#x5BF9;&#x8C61;&#x6216;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x4E0A;&#x83B7;&#x53D6;&#x503C;&#x5E76;&#x8F6C;&#x6362;&#x4E3A;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x8FD4;&#x56DE;&#x3002;
   * &#x9488;&#x5BF9;readonly(reactive(new Map()))&#x4E3A;&#x4EC0;&#x4E48;&#x662F;&#x4ECE;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x4E0A;&#x83B7;&#x53D6;&#x503C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x4ECE;Map&#x5BF9;&#x8C61;&#x4E0A;&#x83B7;&#x53D6;&#x503C;&#x5462;&#xFF1F;
   * &#x8FD9;&#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x6301;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x4ECE;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x4E2D;&#x83B7;&#x53D6;&#x503C;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x5728;&#x7ECF;&#x8FC7;readonly&#x7684;&#x5904;&#x7406;&#x5219;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x5C31;&#x662F;readonly(reactive({value: &apos;foo&apos;}))&#x3002;
   */</span> 
  <span class="hljs-keyword">if</span> (has.call(rawTarget, key)) {
    <span class="hljs-keyword">return</span> wrap(target.get(key))
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (has.call(rawTarget, rawKey)) {
    <span class="hljs-keyword">return</span> wrap(target.get(rawKey))
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (target !== rawTarget) {
    <span class="hljs-comment">/**
     * &#x9488;&#x5BF9;readonly(reactive(new Map()))&#xFF0C;&#x5373;&#x4F7F;&#x6CA1;&#x6709;&#x5339;&#x914D;&#x7684;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x4E5F;&#x8981;&#x8DDF;&#x8E2A;&#x5BF9;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x67D0;&#x952E;&#x7684;&#x4F9D;&#x8D56;&#x4FE1;&#x606F;
     * const state = reactive(new Map())
     * const readonlyState = readonly(state)
     * 
     * effect(() =&gt; {
     *  console.log(readonlyState.get(&apos;foo&apos;))
     * })
     * // &#x56DE;&#x663E; undefined
     * state.set(&apos;foo&apos;, 1)
     * // &#x56DE;&#x663E; 1
     */</span>
    target.get(key)
  }

  <span class="hljs-comment">// &#x5565;&#x90FD;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x5C31;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;undefined&#xFF0C;&#x6240;&#x4EE5;&#x5565;&#x90FD;&#x4E0D;&#x7528;&#x5199;</span>
}
</code></pre>
<h3 id="map&#x548C;set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;"><a name="map&#x548C;set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;" class="anchor-navigation-ex-anchor" href="#map&#x548C;set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.2. Map&#x548C;Set&#x7684;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">size</span>(<span class="hljs-params">target: IterableCollections, isReadonly = <span class="hljs-literal">false</span></span>) </span>{
  <span class="hljs-comment">// &#x9488;&#x5BF9;readonly(reactive(new Map())) &#x6216; readonly(reactive(new Set()))&#x53EA;&#x9700;&#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x5373;&#x53EF;&#xFF0C;&#x56E0;&#x6B64;reactive&#x5BF9;&#x8C61;&#x4E5F;&#x4F1A;&#x5BF9;size&#x7684;&#x8BBF;&#x95EE;&#x8FDB;&#x884C;&#x76F8;&#x540C;&#x7684;&#x64CD;&#x4F5C;&#x3002;</span>
  target = (target as <span class="hljs-built_in">any</span>)[ReactiveFlags.RAW]
  <span class="hljs-comment">// &#x8DDF;&#x8E2A;ITERATE_KEY&#x5373;&#x6240;&#x6709;&#x4FEE;&#x6539;size&#x7684;&#x64CD;&#x4F5C;&#x5747;&#x4F1A;&#x89E6;&#x53D1;&#x8BBF;&#x95EE;size&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
  !iReadonly &amp;&amp; track(toRaw(target), TrackOpTypes.ITERATE, ITERATE_KEY)
  <span class="hljs-comment">/**
   * &#x7531;&#x4E8E;size&#x4E3A;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;&#x56E0;&#x6B64;&#x82E5;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x9012;receiver(&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;)&#xFF0C;&#x800C;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x5E76;&#x6CA1;&#x6709;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x62A5;&#x5F02;&#x5E38;``&#x3002;&#x56E0;&#x6B64;&#x9700;&#x8981;&#x6700;&#x7EC8;&#x5C06;Map&#x6216;Set&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;size&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;&#x7684;this&#x53D8;&#x91CF;&#x3002;
   */</span>
  <span class="hljs-keyword">return</span> Reflect.get(target, <span class="hljs-string">&apos;size&apos;</span>, target)
}
</code></pre>
<h3 id="map&#x548C;set&#x7684;has&#x65B9;&#x6CD5;"><a name="map&#x548C;set&#x7684;has&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#map&#x548C;set&#x7684;has&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.3. Map&#x548C;Set&#x7684;has&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">has</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: CollectionTypes, key: unknown, isReadonly = <span class="hljs-literal">false</span></span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> target = (<span class="hljs-keyword">this</span> as <span class="hljs-built_in">any</span>)[ReactiveFlags.RAW]
  <span class="hljs-keyword">const</span> rawTarget = toRaw(target)
  <span class="hljs-keyword">const</span> rawKey = toRaw(key)
  <span class="hljs-comment">// &#x548C;get&#x65B9;&#x6CD5;&#x4EE3;&#x7406;&#x4E00;&#x6837;&#xFF0C;&#x82E5;key&#x4E3A;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x5219;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x6216;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x952E;&#x7684;&#x952E;&#x503C;&#x5BF9;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;&#x8BBF;&#x95EE;has&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
  <span class="hljs-keyword">if</span> (key !== rawKey) {
    !isReadonly &amp;&amp; track(rawTarget, TrackOpTypes.HAS, key)
  }
  !isReadonly &amp;&amp; track(rawTarget, TrackOpTypes.HAS, rawKey)

  <span class="hljs-keyword">return</span> key === rawKey
    ? target.has(key)
    : target.has(key) || target.has(rawKey)
}
</code></pre>
<h3 id="set&#x7684;add&#x65B9;&#x6CD5;"><a name="set&#x7684;add&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#set&#x7684;add&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.4. Set&#x7684;add&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: SetTypes, value: unknown</span>) </span>{
  value = toRaw(value)
  <span class="hljs-keyword">const</span> target = toRaw(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">const</span> proto = getProto(target)
  <span class="hljs-keyword">const</span> hadKey = proto.has.call(target, value)
  <span class="hljs-comment">// &#x5F53;Set&#x5BF9;&#x8C61;&#x4E2D;&#x6CA1;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#x65F6;&#x5219;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;ITERATE_KEY&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x56E0;&#x6B64;ADD&#x64CD;&#x4F5C;&#x4F1A;&#x5F71;&#x54CD;Set&#x5BF9;&#x8C61;&#x7684;&#x957F;&#x5EA6;</span>
  <span class="hljs-keyword">if</span> (!hadKey) {
    target.add(value)
    trigger(target, TriggerOpTypes.ADD, value, value)
  }

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>
}
</code></pre>
<h3 id="map&#x7684;set&#x65B9;&#x6CD5;"><a name="map&#x7684;set&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#map&#x7684;set&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.5. Map&#x7684;set&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: MapTypes, key: unknown, value: unknown</span>) </span>{
  value = toRaw(value)
  <span class="hljs-keyword">const</span> target = toRaw(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">const</span> { has, <span class="hljs-keyword">get</span> } = getProto(target)

  <span class="hljs-comment">// &#x5206;&#x522B;&#x68C0;&#x67E5;&#x4EE3;&#x7406;&#x548C;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;key&#x662F;&#x5426;&#x5B58;&#x5728;&#x4E8E;Map&#x5BF9;&#x8C61;&#x4E2D;</span>
  <span class="hljs-keyword">let</span> hadKey = has.call(target, key)
  <span class="hljs-keyword">if</span> (!hadKey) {
    key = toRaw(key)
    hadKey = has.call(target.key)
  }

  <span class="hljs-keyword">const</span> oldValue = <span class="hljs-keyword">get</span>.call(target, key)
  target.set(key, value)
  <span class="hljs-keyword">if</span> (!hadKey) {
    <span class="hljs-comment">// &#x5F53;Map&#x5BF9;&#x8C61;&#x4E2D;&#x6CA1;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#x65F6;&#x5219;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;ITERATE_KEY&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x56E0;&#x6B64;ADD&#x64CD;&#x4F5C;&#x4F1A;&#x5F71;&#x54CD;Map&#x5BF9;&#x8C61;&#x7684;&#x957F;&#x5EA6;</span>
    trigger(target, TriggerOpTypes.ADD, key, value)
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (hasChanged(value, oldValue)) {
    <span class="hljs-comment">// &#x5982;&#x679C;&#x65B0;&#x65E7;&#x503C;&#x4E0D;&#x540C;&#x5219;&#x89E6;&#x53D1;&#x4FEE;&#x6539;&#xFF0C;&#x4F9D;&#x8D56;&#x8BE5;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5C06;&#x88AB;&#x89E6;&#x53D1;</span>
    trigger(target, TriggerOpTypes.SET, key, value, oldValue)
  }
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;<code>get</code>&#x548C;<code>has</code>&#x65B9;&#x6CD5;&#x4E2D;&#x4F1A;&#x540C;&#x65F6;&#x8DDF;&#x8E2A;&#x4EE3;&#x7406;&#x548C;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x5143;&#x7D20;&#x53D8;&#x5316;&#xFF0C;&#x800C;<code>set</code>&#x65B9;&#x6CD5;&#x5219;&#x53EA;&#x4F1A;&#x89E6;&#x53D1;&#x67E5;&#x627E;&#x5230;&#x7684;&#x4EE3;&#x7406;&#x6216;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x5143;&#x7D20;&#x53D8;&#x5316;&#x3002;</p>
<h3 id="deleteentry&#x65B9;&#x6CD5;"><a name="deleteentry&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#deleteentry&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.6. deleteEntry&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">deleteEntry</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: CollectionTypes, key: unknown</span>) </span>{
  <span class="hljs-keyword">const</span> target = toRaw(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">const</span> { has, <span class="hljs-keyword">get</span> } = getProto(target)
  <span class="hljs-keyword">let</span> hadKey = has.call(target, key)
  <span class="hljs-comment">// &#x5206;&#x522B;&#x68C0;&#x67E5;&#x4EE3;&#x7406;&#x548C;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;key&#x662F;&#x5426;&#x5B58;&#x5728;&#x4E8E;Map/Set&#x5BF9;&#x8C61;&#x4E2D;</span>
  <span class="hljs-keyword">let</span> hadKey = has.call(target, key)
  <span class="hljs-keyword">if</span> (!hadKey) {
    key = toRaw(key)
    hadKey = has.call(target.key)
  }

  <span class="hljs-comment">// &#x5982;&#x679C;&#x5F53;&#x524D;&#x64CD;&#x4F5C;&#x7684;&#x662F;Map&#x5BF9;&#x8C61;&#x5219;&#x83B7;&#x53D6;&#x65E7;&#x503C;</span>
  <span class="hljs-keyword">const</span> oldValue = <span class="hljs-keyword">get</span> ? <span class="hljs-keyword">get</span>.call(target, key) : <span class="hljs-literal">undefined</span>
  <span class="hljs-keyword">const</span> result = target.delete(key)
  <span class="hljs-keyword">if</span> (hadKey) {
    trigger(target, TriggerOpTypes.DELETE, key, <span class="hljs-literal">undefined</span>, oldValue)
  }
  <span class="hljs-keyword">return</span> result
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;<code>get</code>&#x548C;<code>has</code>&#x65B9;&#x6CD5;&#x4E2D;&#x4F1A;&#x540C;&#x65F6;&#x8DDF;&#x8E2A;&#x4EE3;&#x7406;&#x548C;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x5143;&#x7D20;&#x53D8;&#x5316;&#xFF0C;&#x800C;<code>deleteEntry</code>&#x65B9;&#x6CD5;&#x5219;&#x53EA;&#x4F1A;&#x89E6;&#x53D1;&#x67E5;&#x627E;&#x5230;&#x7684;&#x4EE3;&#x7406;&#x6216;&#x975E;&#x4EE3;&#x7406;&#x7248;&#x672C;&#x7684;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x5143;&#x7D20;&#x53D8;&#x5316;&#x3002;</p>
<h3 id="map&#x548C;set&#x7684;clear&#x65B9;&#x6CD5;"><a name="map&#x548C;set&#x7684;clear&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#map&#x548C;set&#x7684;clear&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.7. Map&#x548C;Set&#x7684;clear&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">clear</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: IterableCollections</span>) </span>{
  <span class="hljs-keyword">const</span> target = toRaw(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">const</span> hadItems = target.size !== <span class="hljs-number">0</span>
  <span class="hljs-keyword">const</span> oldTarget = <span class="hljs-literal">undefined</span>
  <span class="hljs-keyword">const</span> result = target.clear()
  <span class="hljs-keyword">if</span> (hadItems) {
    trigger(target, TriggerOpTypes.CLEAR, <span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>, oldTarget)
  }
  <span class="hljs-keyword">return</span> result
}
</code></pre>
<h3 id="map&#x548C;set&#x7684;foreach&#x65B9;&#x6CD5;"><a name="map&#x548C;set&#x7684;foreach&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#map&#x548C;set&#x7684;foreach&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.8. Map&#x548C;Set&#x7684;forEach&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createForEach</span>(<span class="hljs-params">isReadonly: <span class="hljs-built_in">boolean</span>, isShallow: <span class="hljs-built_in">boolean</span></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">forEach</span>(<span class="hljs-params">
    <span class="hljs-keyword">this</span>: IterableCollections,
    callback: <span class="hljs-built_in">Function</span>,
    thisArg?: unknown
  </span>) </span>{
    <span class="hljs-keyword">const</span> observed = <span class="hljs-keyword">this</span> as <span class="hljs-built_in">any</span>
    <span class="hljs-keyword">const</span> target = observed[ReactiveFlags.RAW]
    <span class="hljs-keyword">const</span> rawTarget = toRaw(target)
    <span class="hljs-keyword">const</span> wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
    !isReadonly &amp;&amp; track(rawTarget, TrackOpTypes.ITERATE, ITERATE_KEY)
    <span class="hljs-keyword">return</span> target.forEach((value: unknown, key: unknown) =&gt; {
      <span class="hljs-comment">// &#x5C06;key&#x548C;value&#x90FD;&#x8F6C;&#x6362;&#x4E3A;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;</span>
      <span class="hljs-keyword">return</span> callback.call(thisArg, wrap(value), wrap(key), observed)
    })
  }
}
</code></pre>
<p>&#x7531;&#x4E8E;<code>forEach</code>&#x4F1A;&#x904D;&#x5386;&#x6240;&#x6709;&#x5143;&#x7D20;&#xFF08;Map&#x5BF9;&#x8C61;&#x5219;&#x662F;&#x6240;&#x6709;&#x952E;&#x503C;&#x5BF9;&#xFF09;&#xFF0C;&#x56E0;&#x6B64;&#x8DDF;&#x8E2A;<code>ITERATE_KEY</code>&#x5373;Map/Set&#x5BF9;&#x8C61;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x5219;&#x89E6;&#x53D1;<code>forEach</code>&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x3002;</p>
<h2 id="&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;"><a name="&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x65B9;&#x6CD5;</h2>
<p>&#x81F3;&#x6B64;&#x6211;&#x4EEC;&#x8FD8;&#x6CA1;&#x5BF9;<code>entries</code>,<code>values</code>,<code>keys</code>&#x548C;<code>@@iterator</code>&#x8FD9;&#x4E9B;&#x8FD4;&#x56DE;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x4EE3;&#x7406;&#xFF0C;&#x800C;&#x6E90;&#x7801;&#x4E2D;&#x5219;&#x5728;&#x6700;&#x540E;&#x4E3A;<code>mutableInstrumentations</code>&#x6DFB;&#x52A0;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x7684;&#x4EE3;&#x7406;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> iteratorMethods = [<span class="hljs-string">&apos;keys&apos;</span>, <span class="hljs-string">&apos;values&apos;</span>, <span class="hljs-string">&apos;entries&apos;</span>, Symbol.iterator<span class="hljs-comment">/*&#x5C31;&#x662F;@@iterator*/</span>]
iteratorMethods.forEach(method =&gt; {
  mutableInstrumentations[method as <span class="hljs-built_in">string</span>] = createIterableMethod(
    method,
    <span class="hljs-literal">false</span>,
    <span class="hljs-literal">false</span>
  )
})
</code></pre>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createIterableMethod</span>(<span class="hljs-params">
  method: <span class="hljs-built_in">string</span> | symbol,
  isReadonly: <span class="hljs-built_in">boolean</span>,
  isShallow: <span class="hljs-built_in">boolean</span>
</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">
    <span class="hljs-keyword">this</span>: IterableCollections,
    ...args: unknown[]
  </span>): <span class="hljs-title">Iterable</span> &amp; <span class="hljs-title">Iterator</span> </span>{
    <span class="hljs-comment">/**
     * 1. &#x9488;&#x5BF9;readonly(reactive(new Map()))&#x7684;&#x60C5;&#x51B5;&#xFF0C;
     *    target&#x83B7;&#x53D6;&#x7684;&#x662F;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;rawTarget&#x7684;&#x662F;Map&#x6216;Set&#x5BF9;&#x8C61;
     * 2. &#x9488;&#x5BF9;reactive(new Map())&#x7684;&#x60C5;&#x51B5;&#xFF0C;
     *    target&#x548C;rawTarget&#x90FD;&#x662F;&#x6307;&#x5411;Map&#x6216;Set&#x5BF9;&#x8C61;
     */</span> 
    <span class="hljs-keyword">const</span> target = (<span class="hljs-keyword">this</span> as <span class="hljs-built_in">any</span>)[ReactiveFlags.RAW]
    <span class="hljs-keyword">const</span> rawTarget = toRaw(target)

    <span class="hljs-keyword">const</span> targetIsMap = isMap(rawTarget)
    <span class="hljs-keyword">const</span> isPair = method === <span class="hljs-string">&apos;entries&apos;</span> || (method === Symbol.iterator &amp;&amp; targetIsMap)
    <span class="hljs-comment">/**
     * &#x5F53;&#x8C03;&#x7528;&#x7684;&#x662F;Map&#x5BF9;&#x8C61;&#x7684;keys&#x65B9;&#x6CD5;&#xFF0C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5E76;&#x6CA1;&#x6709;&#x8BBF;&#x95EE;&#x503C;&#x5BF9;&#x8C61;&#xFF0C;&#x5373;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x53EA;&#x4F9D;&#x8D56;Map&#x5BF9;&#x8C61;&#x7684;&#x952E;&#x800C;&#x6CA1;&#x6709;&#x4F9D;&#x8D56;&#x503C;&#x3002;
     * &#x800C;&#x952E;&#x53EA;&#x80FD;&#x589E;&#x52A0;&#x6216;&#x5220;&#x9664;&#xFF0C;&#x503C;&#x53EF;&#x589E;&#x52A0;&#x3001;&#x5220;&#x9664;&#x548C;&#x4FEE;&#x6539;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x65F6;&#x5F53;&#x4E14;&#x4EC5;&#x5F53;&#x952E;&#x589E;&#x5220;&#x5373;size&#x5C5E;&#x6027;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x624D;&#x4F1A;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x3002;
     * &#x82E5;&#x4F9D;&#x8D56;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x4FEE;&#x6539;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x503C;&#x4E5F;&#x4F1A;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x3002;
     */</span>
    <span class="hljs-keyword">const</span> isKeyOnly = method === <span class="hljs-string">&apos;keys&apos;</span> &amp;&amp; targetIsMap
    <span class="hljs-keyword">const</span> innerIterator = target[method](...args)
    <span class="hljs-keyword">const</span> wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
    !isReadonly &amp;&amp;
      track(
        rawTarget,
        TrackOpTypes.ITERATE,
        isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
      )

    <span class="hljs-keyword">return</span> {
      <span class="hljs-comment">// &#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;</span>
      next() {
        <span class="hljs-keyword">const</span> { value, done } = innerIterator.next()
        <span class="hljs-keyword">return</span> done
          ? { value, done }
          : {
            value: isPair ? [wrap(value[<span class="hljs-number">0</span>], wrap(value[<span class="hljs-number">1</span>]))] : wrap(value),
            done
          }
      },
      <span class="hljs-comment">// &#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;</span>
      [Symbol.iterator]() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>
      }
    }
  }
}
</code></pre>
<h3 id="&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;iterable-protocol"><a name="&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;iterable-protocol" class="anchor-navigation-ex-anchor" href="#&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;iterable-protocol"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.1. &#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;(iterable protocol)</h3>
<p>&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;(iterable protocol)&#xFF0C;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x8FED;&#x4EE3;&#x5668;(iterator)&#x3002;
&#x5982;&#x4E0B;&#x5185;&#x7F6E;&#x7C7B;&#x578B;&#x90FD;&#x5B9E;&#x73B0;&#x4E86;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#xFF1A;</p>
<ul>
<li>&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x6570;&#x7EC4;</li>
<li>Set</li>
<li>Map</li>
<li>arguments&#x5BF9;&#x8C61;</li>
<li>NodeList&#x7B49;DOM&#x96C6;&#x5408;&#x7C7B;&#x578B;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x7684;&#x8BED;&#x8A00;&#x7279;&#x6027;&#x5C06;&#x4F1A;&#x63A5;&#x6536;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#x8FD4;&#x56DE;&#x7684;&#x8FED;&#x4EE3;&#x5668;</p>
<ul>
<li><code>for...of</code>&#x5FAA;&#x73AF;</li>
<li>&#x6570;&#x636E;&#x89E3;&#x6784;(<code>const [a, b] = [1, 2]</code>)</li>
<li>&#x6269;&#x5C55;&#x64CD;&#x4F5C;&#x7B26;(<code>const a = [1,2], b = [...a]</code>)</li>
<li><code>Array.from()</code></li>
<li>&#x521B;&#x5EFA;Set</li>
<li>&#x521B;&#x5EFA;Map</li>
<li><code>Promise.all()</code>&#x63A5;&#x53D7;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</li>
<li><code>Promise.race()</code>&#x63A5;&#x53D7;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</li>
<li><code>yield*</code>&#x64CD;&#x4F5C;&#x7B26;</li>
</ul>
<p>&#x8BA9;&#x5BF9;&#x8C61;&#x652F;&#x6301;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#x5176;&#x5B9E;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x53EA;&#x9700;&#x5B9E;&#x73B0;&#x8FD4;&#x56DE;&#x8FED;&#x4EE3;&#x5668;&#x7684;<code>[Symbol.iterator]</code>&#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x3002;JavaScript Plain Old Object&#x9ED8;&#x8BA4;&#x5E76;&#x6CA1;&#x6709;&#x652F;&#x6301;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x5B9E;&#x73B0;&#x4EE5;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> iterablizeKeys = (obj: {}) =&gt; {
  <span class="hljs-keyword">if</span> (!obj[Symbol.iterator]) {
    obj[Symbol.iterator] = () =&gt; {
      <span class="hljs-keyword">const</span> keys = <span class="hljs-built_in">Object</span>.keys(obj) as <span class="hljs-keyword">const</span>
      <span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>

      <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;</span>
      <span class="hljs-keyword">return</span> {
        next() {
          <span class="hljs-keyword">return</span> { value: keys[i++], done: i &gt; keys.length }
        }
      }
    }
  }

  <span class="hljs-keyword">return</span> obj
} 

<span class="hljs-keyword">const</span> iterableObj = iterablizeKeys({a: <span class="hljs-number">1</span>, b: <span class="hljs-number">2</span>})
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> item of iterableObj) {
  <span class="hljs-built_in">console</span>.log(item)
}
<span class="hljs-comment">// &#x56DE;&#x663E; a </span>
<span class="hljs-comment">// &#x56DE;&#x663E; b</span>
<span class="hljs-built_in">Array</span>.from(iterableObj) <span class="hljs-comment">// &#x8FD4;&#x56DE; [&apos;a&apos;, &apos;b&apos;]</span>
</code></pre>
<h3 id="&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;iterator-protocol"><a name="&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;iterator-protocol" class="anchor-navigation-ex-anchor" href="#&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;iterator-protocol"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.2. &#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;(iterator protocol)</h3>
<p>&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;(iterator protocol)&#xFF0C;&#x63D0;&#x4F9B;&#x4E0D;&#x63A5;&#x53D7;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#x5E76;&#x8FD4;&#x56DE;<code>IteratorResult</code>&#x5BF9;&#x8C61;&#x7684;<code>next</code>&#x65B9;&#x6CD5;&#xFF0C;&#x800C;<code>IteratorResult</code>&#x5BF9;&#x8C61;&#x5305;&#x542B;&#x6307;&#x5411;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x7684;<code>value</code>&#x5C5E;&#x6027;&#x548C;&#x8868;&#x793A;&#x8FED;&#x4EE3;&#x662F;&#x5426;&#x5DF2;&#x7ED3;&#x675F;&#x7684;<code>done</code>&#x5C5E;&#x6027;&#xFF0C;&#x5F53;<code>done</code>&#x5C5E;&#x6027;&#x503C;&#x4E3A;<code>true</code>&#x65F6;&#x8868;&#x793A;&#x8FED;&#x4EE3;&#x5DF2;&#x7ED3;&#x675F;&#x3002;
&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#x6B63;&#x5982;&#x4E0A;&#x9762;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x90A3;&#x6837;&#xFF0C;&#x4E0D;&#x8FC7;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x5C06;&#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#x548C;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4E0A;&#x5B9E;&#x73B0;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> iterablizeKeys = (obj: {}) =&gt; {
  <span class="hljs-keyword">if</span> (!obj[Symbol.iterator]) {
    <span class="hljs-keyword">let</span> iteratorState = {
      keys: []
      i: <span class="hljs-number">0</span>
    }
    <span class="hljs-comment">// &#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;</span>
    obj.next = () =&gt; ({ value: iteratorState.keys[iteratorState.i++], done: iteratorState.i &gt; iteratorState.key.length })

    <span class="hljs-comment">// &#x53EF;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;</span>
    obj[Symbol.iterator] = () =&gt; {
      iteratorState.keys = <span class="hljs-built_in">Object</span>.keys(obj) as <span class="hljs-keyword">const</span>
      iteratorState.i = <span class="hljs-number">0</span>

      <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;</span>
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>
    }
  }

  <span class="hljs-keyword">return</span> obj
} 

<span class="hljs-keyword">const</span> iterableObj = iterablizeKeys({a: <span class="hljs-number">1</span>, b: <span class="hljs-number">2</span>})
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> item of iterableObj) {
  <span class="hljs-built_in">console</span>.log(item)
}
<span class="hljs-comment">// &#x56DE;&#x663E; a </span>
<span class="hljs-comment">// &#x56DE;&#x663E; b</span>
<span class="hljs-built_in">Array</span>.from(iterableObj) <span class="hljs-comment">// &#x8FD4;&#x56DE; [&apos;a&apos;, &apos;b&apos;]</span>
</code></pre>
<h2 id="&#x5C0F;&#x7ED3;"><a name="&#x5C0F;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x5C0F;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x5C0F;&#x7ED3;</h2>
<p>&#x672C;&#x7BC7;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x9010;&#x884C;&#x9605;&#x8BFB;&#x6E90;&#x7801;&#x4E86;&#x89E3;&#x5230;reactive&#x5982;&#x4F55;&#x5904;&#x7406;Map&#x548C;Set&#x5BF9;&#x8C61;&#x4E86;&#xFF0C;&#x4E0B;&#x4E00;&#x7BC7;&#x6211;&#x4EEC;&#x5C06;&#x5F00;&#x59CB;&#x4EE5;<code>effect</code>&#x4E3A;&#x5165;&#x53E3;&#x8FDB;&#x4E00;&#x6B65;&#x4E86;&#x89E3;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x662F;&#x5982;&#x4F55;&#x901A;&#x8FC7;<code>track</code>&#x548C;<code>trigger</code>&#x8BB0;&#x5F55;&#x4F9D;&#x8D56;&#x548C;&#x89E6;&#x53D1;&#x7684;&#x3002;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:47:18
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1_reactive_object_array.html" class="navigation navigation-prev " aria-label="Previous page: 逐行解读reactive如何构建对象和数组的响应式对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3_register_side_effect.html" class="navigation navigation-next " aria-label="Next page: 逐行解读effect如何注册副作用函数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"逐行解读reactive如何构建Map和Set的响应式对象","level":"3.3","depth":1,"next":{"title":"逐行解读effect如何注册副作用函数","level":"3.4","depth":1,"path":"2_reactivity_system/3_register_side_effect.md","ref":"2_reactivity_system/3_register_side_effect.md","articles":[]},"previous":{"title":"逐行解读reactive如何构建对象和数组的响应式对象","level":"3.2","depth":1,"path":"2_reactivity_system/1_reactive_object_array.md","ref":"2_reactivity_system/1_reactive_object_array.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"2_reactivity_system/2_reactive_map_set.md","mtime":"2022-04-26T01:47:18.796Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

